﻿using GalaSoft.MvvmLight;
using KPI.Comm;
using KPI.Entity;
using KPI.UIBase;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using TYBase;

namespace CssdAssistantClient.Views
{
    public class QuestionInfoModel : VMBase
    {
        public QuestionInfoModel()
        {

        }

        private string _Title;
        public string Title { get => _Title; set => Set("Title", ref _Title, value); }

        private string _CreatorName;
        public string CreatorName { get => _CreatorName; set => Set("CreatorName", ref _CreatorName, value); }

        private string _CreateTime;
        public string CreateTime { get => _CreateTime; set => Set("CreateTime", ref _CreateTime, value); }

        private int _QtType;
        public int QtType { get => _QtType; set => Set("QtType", ref _QtType, value); }

        private string _QtTypeStr;
        public string QtTypeStr { get => _QtTypeStr; set => Set("QtTypeStr", ref _QtTypeStr, value); }



        private ObservableCollection<OptionModel> _ListOption = new ObservableCollection<OptionModel>();
        public ObservableCollection<OptionModel> ListOption { get { return _ListOption; } set { Set("ListOption", ref _ListOption, value); } }






        private ObservableCollection<StringStringData> _ListTag = new ObservableCollection<StringStringData>();
        public ObservableCollection<StringStringData> ListTag { get { return _ListTag; } set { Set("ListTag", ref _ListTag, value); } }

        public QuestionBankInfoVO Data { get; private set; } = new QuestionBankInfoVO();

        private int _Score;
        public int Score { get => _Score; set => Set("Score", ref _Score, value); }

        private int _UserScore;
        public int UserScore { get => _UserScore; set => Set("UserScore", ref _UserScore, value); }



        private string _Tag;
        public string Tag { get => _Tag; set => Set("Tag", ref _Tag, value); }



        public virtual void FromData(QuestionBankInfoVO data)
        {
            CreatorName = data.creatorName;
            Title = data.title;
            CreateTime = data.createTime.NullAsTimeStr();
            QtType = data.qtType;
            QtTypeStr = EnumHelper.QtTypeAsString(data.qtType);
            Score = data.score;
            Data = data;

            ListOption.Clear();
            ListTag.Clear();
            data.listTagInfo?.ForEach(x =>
            {
                ListTag.Add(new StringStringData { Key = x.tagId, Value = x.tagName });
            });
            Tag = string.Join(", ", data.listTagInfo?.Select(x => x.tagName));
            data.listItemInfo.ForEach(x =>
            {
                OptionModel optionModel = new OptionModel() { Data = x, OptionName = x.content, Score = x.score }; 
                if (data.listUserAnswer != null)
                {
                    foreach (var userAnswer in data.listUserAnswer)
                    {
                        if (x.qbItemId == userAnswer.answerId)
                        {
                            optionModel.IsSelected = true;
                            break;
                        }
                    }
                }
                ListOption.Add(optionModel);
            });
            if (data.listItemInfo == null || data.listItemInfo.Count == 0)
            {
                if (data.qtType == (int)(EnumQtType.TrueFalse))
                {
                    ListOption.Add(new OptionModel { OptionName = "是" });
                    ListOption.Add(new OptionModel { OptionName = "否" });

                }
                else if (data.qtType == (int)(EnumQtType.SingleSelect) || data.qtType == (int)(EnumQtType.MutilSelect))
                {
                    if (ListOption.Count == 0)
                    {
                        ListOption.Add(new OptionModel { OptionName = "" });
                        ListOption.Add(new OptionModel { OptionName = "" });
                    }
                }
            }
        }
    }

    public class OptionModel : VMBase
    {
        private string _OptionName;
        public string OptionName { get => _OptionName; set => Set("OptionName", ref _OptionName, value); }

        private int _Score;
        public int Score { get => _Score; set => Set("Score", ref _Score, value); }

        private bool _IsSelected;
        public bool IsSelected { get => _IsSelected; set => Set("IsSelected", ref _IsSelected, value); }


        public QuestionItemInfoVO Data { get; internal set; }

        internal static OptionModel FromData(QuestionItemInfoVO x)
        {
            OptionModel option = new OptionModel()
            {
                OptionName = x.content,
                Score = x.score,
            };
            return option;
        }
    }
}
